<?php
/**
 * APC缓存引擎
 *
 * @since 1.0
 * @package if
 * @subpackage module.cache
 */
class IApcCacheEngine extends IAbstractCacheEngine {
	private $settings = array();

	
	function init(array $settings = null) {
		if (is_array($settings)) {
			$this->settings = $settings;
		}
		$enabled = (bool)ini_get("apc.enabled");
		if (!$enabled) {
			throw new ICacheException("apc extension must be loaded in php.ini", 0x100065);
		}
	}
	
	function clear($expired = false) {
		if (!$expired) {
			return apc_clear_cache("user");
		}
		$users = apc_cache_info("user");
		$list = $users["cache_list"];
		if (!empty($list)) {
			foreach ($list as $item) {
				$this->delete($item["info"]);
			}
		}
		return true;
	}
	
	function delete($key) {
		return apc_delete($key);
	}
	
	function _entry($key) {
		$array = apc_fetch($key);
		if (!is_array($array)) {
			if ($array) {
				$this->delete($key);
			}
			return null;
		}
		
		$entry = new ICacheEntry($array);
		$entry->setEngine($this);
		if ($entry->expired()) {
			$this->delete($key);
			return null;
		}
		return $entry;
	}
	
	function _writeEntry(ICacheEntry $entry) {
		$array = $entry->asArray();
		return apc_store($entry->key(), $array, $entry->ttl());
	}
}


?>